package org.cloud.simple.helloworld;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class HtmlToPdfUtil {
	// jdk8 可以直接使用 process.waitFor(1, TimeUnit.MINUTES);但无转换日志。
	public static void processBuilder(String htmlpath, String pdfpath) {
		BufferedReader stdout = null;
		try {
			List<String> command = new ArrayList<>();
			command.add("wkhtmltopdf");
			command.add(htmlpath);
			command.add(pdfpath);
			ProcessBuilder pb = new ProcessBuilder(command);
			String line = null;
			pb.redirectErrorStream(true);//合并error stream
			Process process = pb.start();
			stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
			while ((line = stdout.readLine()) != null) {
				LogUtil.getAppLogger().info(line);
			}
			int i = process.waitFor();
			LogUtil.getAppLogger().info("html to pdf success! " + i);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (stdout != null) {
				try {
					stdout.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}
